package flashpromotion

import (
	"context"
	"encoding/json"

	"github.com/blog-zero/api/admin/internal/common/errorx"
	"github.com/blog-zero/api/admin/internal/svc"
	"github.com/blog-zero/api/admin/internal/types"
	"github.com/blog-zero/rpc/sms/smsclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type FlashPromotionUpdateStatusLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewFlashPromotionUpdateStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FlashPromotionUpdateStatusLogic {
	return &FlashPromotionUpdateStatusLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *FlashPromotionUpdateStatusLogic) FlashPromotionUpdateStatus(req *types.UpdateStatusFlashPromotionReq) (*types.UpdateStatusFlashPromotionResp, error) {
	_, err := l.svcCtx.FlashPromotionService.FlashPromotionUpdateStatus(l.ctx, &smsclient.FlashPromotionUpdateStatusReq{
		Id:     req.Id,
		Status: req.Status,
	})

	if err != nil {
		reqStr, _ := json.Marshal(req)
		logx.WithContext(l.ctx).Errorf("更新秒杀活动失败,参数:%s,异常:%s", reqStr, err.Error())
		return nil, errorx.NewDefaultError("更新秒杀活动失败")
	}

	return &types.UpdateStatusFlashPromotionResp{
		Code:    "000000",
		Message: "更新秒杀成功",
	}, nil
}
